package com.lm.apenai.service;

import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.completion.chat.ChatCompletionChoice;
import com.theokanning.openai.service.OpenAiService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ChatService {

    private final OpenAiService openAiService;

    public ChatService(OpenAiService openAiService) {
        this.openAiService = openAiService;
    }

    public String chat(String userMessage) {
        List<ChatMessage> messages = Arrays.asList(
                new ChatMessage("system", "你是一个专业的Java架构师助手。"),
                new ChatMessage("user", userMessage)
        );

        ChatCompletionRequest request = ChatCompletionRequest.builder()
                .model("gpt-3.5-turbo")
                .messages(messages)
                .temperature(0.7)
                .maxTokens(500)
                .build();

        List<ChatCompletionChoice> choices = openAiService.createChatCompletion(request).getChoices();

        return choices.get(0).getMessage().getContent();
    }
}